/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kazares.filter;

//import Clases.ResponseWrapper;
import java.io.CharArrayWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponseWrapper;


/**
 *
 * @author Windows
 */
public class AppFilter implements Filter {

    private FilterConfig filterConfig;

    public AppFilter() {
    }

    public void init(FilterConfig config) {
        this.filterConfig = config;
    }

    public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletResponse newResponse = response;
        if (request instanceof HttpServletRequest) {
            newResponse = new CharResponseWrapper((HttpServletResponse) response);
        }

        
        HttpServletRequest request1 = (HttpServletRequest) request;
        String uri = request1.getRequestURI();
        if (uri.matches(".+[.]jsp")) {
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            httpResponse.sendRedirect(request1.getContextPath() + "/?m=404");
        } else {
            //chain.doFilter(request, response);
            chain.doFilter(request, newResponse);
        }
        
        //chain.doFilter(request, newResponse);

        if (newResponse instanceof CharResponseWrapper) {
            String text = newResponse.toString();
            if (text != null) {
                //sustituye espacios y saltos de linea  por solo un espacio en blanco
                text = text.replaceAll("[>]\\s+[<]", "><").replaceAll("\\s+", " ");
                //text = text.replaceAll("<!--(.|\\s)?-->", "");
                response.getWriter().write(text);
            }
        }
    }
    protected FilterConfig config;

    public void destroy() {
    }
}
class CharResponseWrapper extends HttpServletResponseWrapper {

    protected CharArrayWriter charWriter;
    protected PrintWriter writer;
    protected boolean getOutputStreamCalled;
    protected boolean getWriterCalled;

    public CharResponseWrapper(HttpServletResponse response) {
        super(response);

        charWriter = new CharArrayWriter();
    }

    public ServletOutputStream getOutputStream() throws IOException {
        if (getWriterCalled) {
            throw new IllegalStateException("getWriter already called");
        }

        getOutputStreamCalled = true;
        return super.getOutputStream();
    }

    public PrintWriter getWriter() throws IOException {
        if (writer != null) {
            return writer;
        }
        if (getOutputStreamCalled) {
            throw new IllegalStateException("getOutputStream already called");
        }
        getWriterCalled = true;
        writer = new PrintWriter(charWriter);
        return writer;
    }

    public String toString() {
        String s = null;

        if (writer != null) {
            s = charWriter.toString();
        }
        return s;
    }
}